package com.yunji.product.business;

import com.yunji.common.base.BaseBusiness;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.domain.BrandDO;
import com.yunji.product.params.BrandParam;
import com.yunji.product.service.IProductBrandService;
import com.yunji.product.service.IProductService;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.product.business
 * @Title: ProductBrandBusiness
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-14 10:10:27      V1.0        HuaAo       新建类
 */
@Component
public class ProductBrandBusiness extends BaseBusiness {

    @Resource
    IProductBrandService productBrandService;

    @Resource
    IProductService productService;

    /**
     * @Title 获取品牌列表
     * @Desc
     * @Date 2024-08-13 18:11:42.852
     * @Return null
     */
    public List<BrandDO> search(BrandParam param) {
        return productBrandService.search(param);
    }

    /**
     * @Title 新增品牌单位
     * @Desc
     * @Date 2024-08-13 17:26:45.779
     * @Return null
     */
    public Long create(BrandDO brand) {
        checkParam(brand);
        Assert.isTrue(productBrandService.create(brand), "新增失败");
        return brand.getBrandId();
    }

    /**
     * @Title 查看商品品牌详情
     * @Desc
     * @Date 2024-08-13 17:55:51.995
     * @Return null
     */
    public BrandDO select(Long id) {
        return productBrandService.select(id);
    }


    public void update(BrandDO brand) {
        checkParam(brand);
        Assert.isTrue(productBrandService.update(brand), "修改失败");
    }

    public void delete(Long... ids) {
        // 有限制条件 有商品品牌则无法删除
        Assert.isTrue((productService.statsOfProductBrand(ids) == 0), ErrorEnum.BUSINESS_512003.getText());

        productBrandService.delete(ids);
    }

    public Boolean inUse(Long brandId) {
        return (productService.statsOfProductBrand(brandId) > 0) ? Boolean.TRUE : Boolean.FALSE;
    }

    /*--------------------------------------------校验-------------------------------------------*/

    private void checkParam(BrandDO brand) {
        Assert.isTrue(BeanUtils.isNotNull(brand.getBrandName()), "请输入商品品牌");
        Assert.isTrue(BeanUtils.isNotNull(brand.getSort()), "请输入排序序号");

        String brandName = brand.getBrandName();
        Integer sort = brand.getSort();

        logs.info("【参数校验】商品品牌:{}", brandName);
        logs.info("【校验参数】排序序号:{}", sort);
    }

}